Ext.define('UmlWebEditor.tests.BlockTest', {
	extend: 'UmlWebEditor.tests.Test',
	requires: [
		'UmlWebEditor.controller.WorkSpaceController'
	],
	tests: {
		moveBlockTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');
			var commonDiagrammController = Ext.UmlWebEditor.getController('CommonDiagrammController');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var mainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');
			var toolBar = controller.getMainToolBarWidget();
			var store = workSpaceWidget.getDiagrammStore();

			for(var i = 0; i< store.count();)
				store.removeAt(i);
			store.sync();

			controller.onCreateDiagramm('CommonDiagramm');
			controller.onCreateDiagramm('CommonDiagramm');

			var visibleItem, invisibleItem;
			mainPanel.items.each(function (item) {
				if(item.isVisible())
					visibleItem = item;
				else
					invisibleItem = item;
			});

			var blocksStore = Ext.create('UmlWebEditor.store.BlocksStore');
			blocksStore.load();
			for(var i = 0; i< blocksStore.count();)
				blocksStore.removeAt(i);
			blocksStore.sync();

			commonDiagrammController.addBlock({
				x: 10,
				y: 20,
				type: 'SimpleBlock'
			});
			toolBar.fireEvent('chooseDiagrammEvent', invisibleItem);
			commonDiagrammController.addBlock({
				x: 10,
				y: 20,
				type: 'SimpleBlock'
			});

			workSpaceWidget.destroy();
			console.log('destroy&create');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var toolBar = controller.getMainToolBarWidget();
			var mainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');

			var visibleItem = 0, invisibleItem = 0;
			mainPanel.items.each(function (item) {
				console.log(item.isVisible());
				if(item.isVisible())
					visibleItem = item;
				else
					invisibleItem = item;
			});

			b1 = visibleItem.items.first();
			console.log("b1 moved in (" + visibleItem.getPosition()[0] + ', ' + visibleItem.getPosition()[1] + ')');
			verify(b1.getRelativeX() == 10 && b1.getRelativeY() == 20, "b1 moved in (" + b1.getRelativeX() + ', ' + b1.getRelativeY() + ')');


			toolBar.fireEvent('chooseDiagrammEvent', invisibleItem);
			b2 = invisibleItem.items.first();
			verify(b2.getRelativeX() == 10 && b2.getRelativeY() == 20, "b2 moved in (" + b2.getRelativeX() + ', ' + b1.getRelativeY() + ')');

			workSpaceWidget.destroy();
		},
		createEdgeTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');
			var commonDiagrammController = Ext.UmlWebEditor.getController('CommonDiagrammController');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var mainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');
			var toolBar = controller.getMainToolBarWidget();
			var store = workSpaceWidget.getDiagrammStore();

			for(var i = 0; i< store.count();)
				store.removeAt(i);
			store.sync();

			controller.onCreateDiagramm('CommonDiagramm');

			var diagram;
			mainPanel.items.each(function (item) {
				diagram = item;
			});

			var blocksStore = Ext.create('UmlWebEditor.store.BlocksStore');
			blocksStore.load();
			for(var i = 0; i< blocksStore.count();)
				blocksStore.removeAt(i);
			blocksStore.sync();

			toolBar.fireEvent('chooseDiagrammEvent', diagram);

			commonDiagrammController.addBlock({
				x: 10,
				y: 20,
				type: 'SimpleBlock'
			});
			commonDiagrammController.addBlock({
				x: 100,
				y: 200,
				type: 'SimpleBlock'
			});

			b1 = diagram.items.first();
			b2 = diagram.items.last();

			commonDiagrammController.connectBlocks(b1.getKey(), b2.getKey(), 'SimpleEdge');

			e = diagram.items.last();

			verify(e.self.getName() == 'UmlWebEditor.view.edges.SimpleEdge', 'e not added');

			verify(e.getDestinationKey() == b1.getKey(), 'e source key=' + e.getDestinationKey());

			workSpaceWidget.destroy();
		},
	}
});
